<?php
//自动处理方法
namespace Home\Controller;
use Think\Controller;
class AutohandleController extends CommonController {
    public function index(){
		$oop=M("autoperform");
		$con['isactive']=1;
		I('name') && $con['name']=I('name');

		$list=$oop->where($con)->select();
		!$list && exit("unknow process method！");
		//启动多线程,这样就不用等待所有方法执行完成了
		$asyn=new \Common\Common\AsynHandle();
		echo "start<hr>";	
		foreach($list as $value){
			$process_url=U('Autohandle/'.$value['name'].'@'. $_SERVER['SERVER_NAME']);
			$asyn->Request($process_url);
			dump($process_url);
		}	

		exit("<hr>complete");	
	}

	/**
	*自动退款操作
	*/
	public function autorefund(){
	//找到所有等待退款的交易	
	$oop=D("OrdersView");
	$con['ips_id']=array("neq","[TEST]");
	$con['order_status']=7;
	I("order_id") && $con['order_id']=I("order_id");
	$list=$oop->where($con)->limit(50000)->select();
	//dump($list);exit;
	$oh_oop=M("orderhistory");
	foreach($list as $key=>$value){
		$oh_con['oh_status']=7;
		$oh_con['oh_oid']=$value["order_id"];
		$oh_list=$oh_oop->where($con)->order("oh_id desc")->find();
		if($value['cn_name']){
		$result[$key]['status']=A($value['cn_name'])->refund($value,$oh_list['oh_gateway_amount']);
		//dump($value['cn_name']);
		}else{
		$result[$key]['status']=0;	
		}
		$result[$key]['channel']=$value['cn_name'];
		$result[$key]['refund_amount']=$oh_list['oh_amount'];
		$result[$key]['refund_gateway_amount']=$oh_list['oh_gateway_amount'];
		$result[$key]['order_id']=$value['order_id'];
		$result[$key]['order_time']=$value['order_time'];
		$result[$key]['refund_time']=$oh_list['oh_time'];
		$result[$key]['mid']=$value['order_mid'];
		$result[$key]['site_id']=$value['order_siteid'];		
	}
	dump($result);
	}
	
	/*
	*自动统计每天的交易的状态和数量
	*/
	public function autostatistics(){
		$last_day=date("Y-m-d",strtotime("-1 day"));
		$oop=M("statistics");
		$con['ss_date']=$last_day;
		$list=$oop->where($con)->select();
		//echo $oop->_sql();
		//dump($list);
		$list && exit("already calculated");
		
		$order_oop=M("orders");
		$con1['ips_id']=array("neq","[TEST]");
		$con1['order_time']=array("like","%".$last_day."%");
		//total num
		$data['total_num']=$order_oop->where($con1)->count();
		
		if(!$data['total_num'])
			exit("no records");
			
		//total amount
		$data['total_amount']=$order_oop->where($con1)->sum("base_amount");
		//success_amount
		$con1['order_status']=array("in","2,4");
		$data['success_amount']=$order_oop->where($con1)->sum("base_amount");
		$data["success_amount"]=$data["success_amount"]?$data["success_amount"]:0;
		//success num
		$data['success_num']=$order_oop->where($con1)->count();
		//approval_rate
		$data["approval_rate"]=$data['success_num']/$data["total_num"];
		
		$his_oop=M("orderhistory");
		$con2['oh_time']=array("like","%".$last_day."%");
		$his_list=$his_oop->field("oh_status,count(*) as total")->where($con2)->group("oh_status")->select();
		//echo $his_oop->_sql();
		//dump($his_list);
		foreach($his_list as $value){
			$data['status_num'].=order_status($value['oh_status'])."=>".$value['total']."&nbsp;|&nbsp;";
		}

		
		$data['ss_date']=$last_day;
		//dump($data);exit;
		$oop->add($data);
		exit("over");
		//echo $data['status_num'];
		
	}
	
	/**
	*自动备份数据库的功能
	*/
	public function autobackup(){
		parent::backup();
	}
	
	/**
	*自动更新汇率
	*/
	public function autoroe(){
		
	}
	
	/**
	*自动捕捉
	*/
	public function autocapture(){
		
	}
	
	/**
	*自动取消，pending的交易两天后如果不处理，会自动取消
	*/
	public function autovoid(){
		$oop=M("orders");
		$con['ips_id']=array("neq","[TEST]");
		$con['order_status']=1;
		$con['_string']="TIMESTAMPDIFF( DAY , `order_time`, now( )) >2";//超过2天的
		$list=$oop->where($con)->select();
		//echo $oop->_sql();
		$i=0;
		foreach($list as $value){
			$order=new \Common\Common\OrderProcess($value["order_id"]);
			$result=$order->toVoid();
			if($result)
				$i++;
		}
		exit("成功取消交易数：".$i);
	}
	
	/**
	*异步通知(每5分钟通知一次)
	*/
	public function autonotify(){
		/*	
		"order_status": 6,
        "oid": "1",
        "pid": 2879,
        "order_amount": "1.00",
        "order_currency": "USD",
        "hash_info": "be17b8c545f13cddca1dd7ed05defb93",
        "syn_url": "http%3A%2F%2Fwww.baidu.com"
		*/
	$oop=M("orders");
	$con['order_asyn_status']=0;//没有收到通知的
	$con['order_asyn_count']=array("lt",3);//通知次数小于3
	 I("order_id") && $con['order_id']=I("order_id"); //如果有订单号，则只针对此订单号通知
	$list=$oop->where($con)->limit(50000)->select();//至多查询50000条，多了为内存溢出
	//echo $oop->_sql();
	//dump($list);
	$i=0;
	$j=0;
	$handler=new \Common\Common\AsynHandle();
	foreach($list as $value){
	$post_arr=array(
	"order_status"=>$value["order_status"],
	"oid"=>$value["mh_oid"],
	"pid"=>$value["order_id"],
	"order_amount"=>$value["order_amount"],
	"order_currency"=>$value["order_currency"],
	"syn_url"=>urlencode($value["order_syn_url"]),
	"hash_info"=>$value["order_signature_info"]
	);
	$result=$handler->Get($value["order_asyn_url"], $cookie=array(), $post_arr, $timeout=30);

	if($result==100){//如果有返回，说明接收成功
	$value['order_asyn_status']=1;
	$i++;
	}else{
	$value["order_asyn_count"]++;	
	$j++;
	}
	$oop->save($value);
	}
	echo "通知有返回：".$i."<hr>";
	echo "通知但无返回：".$j."<hr>";
	exit("Over");
	}
	
}

